package org.myself.meeting.service;

import org.myself.meeting.mapper.FileOperationMapper;
import org.myself.meeting.model.FileOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;

@Service
public class FileOperationService {

    @Autowired
    FileOperationMapper fileOperationMapper;

    public List<FileOperation> getAllFiles() {
        return fileOperationMapper.getAllFiles();
    }

    public Integer addFiles(FileOperation fileOperation) {
        return fileOperationMapper.addFiles(fileOperation);
    }

    public FileOperation getFileById(Integer id) {
        return fileOperationMapper.getFileById(id);
    }

    public Integer deleteFileById(Integer id) {
        FileOperation fileOperation = fileOperationMapper.getFileById(id);
        String fileName = fileOperation.getFatherpath()+ File.separator +fileOperation.getFilename();
        File getfile = new File(fileName);
        Boolean result = getfile.delete();
        if(result == false){
            return 0;
        }
        return fileOperationMapper.deleteFileById(id);
    }
}
